# === 前端构建阶段 ===
FROM node:18-alpine AS builder

# 安装 pnpm
RUN npm install -g pnpm

# 配置pnpm使用淘宝镜像源，但对markmap相关包使用原始源
RUN pnpm config set registry https://registry.npmmirror.com/ && \
    pnpm config set @markmap:registry https://registry.npmjs.org/ && \
    pnpm config set fetch-timeout 300000

# 设置工作目录
WORKDIR /app

# 首先只复制依赖相关文件
COPY ./BillNote_frontend/package.json ./BillNote_frontend/pnpm-lock.yaml* ./

# 单独安装依赖
RUN pnpm install || \
    # 如果使用镜像源失败，回退到原始npm源
    (pnpm config set registry https://registry.npmjs.org/ && pnpm install)

# 然后再复制其他源代码
COPY ./BillNote_frontend /app

# 构建
RUN pnpm run build

# --- 阶段2：使用 nginx 作为静态服务器 ---
FROM nginx:1.25-alpine

# 删除默认配置（可选）
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY ./BillNote_frontend/deploy/default.conf /etc/nginx/conf.d/default.conf

# 拷贝构建产物
COPY --from=builder /app/dist /usr/share/nginx/html